﻿using Awsc.DataAcquisitionSys.Core.Common.Extensions;
using Awsc.DataAcquisitionSys.Core.DataAcquisition;
using Awsc.DataAcquisitionSys.Core.Model.Config;
using Awsc.DataAcquisitionSys.Core.Models.ServiceModels;

namespace Awsc.DataAcquisitionSys.Core.Services.DataAcquisition.DataCollectors.Sensors
{
    internal class NeutralPointSensor : Sensor
    {
        private readonly RegisterConfig config;
        private readonly NeutralPointSensorConfig neutralPointSensorConfig;
        public NeutralPointSensor(AbstractSensorDataCollectionContext abstractSensorDataCollectionContext)
        {
            config = abstractSensorDataCollectionContext.SensorConfig as RegisterConfig;
            neutralPointSensorConfig = config.GetAdditionConfig<NeutralPointSensorConfig>();
        }

        protected override void CalculateValue()
        {
            //某一个为空则重新初始化
            var casingPressure = GlobalDeviceManager.Instance.GetSensorValue("套压");
            if (casingPressure != null)
            {
                //上顶力
                double f = 7.854 * Math.Pow(10, -2) * casingPressure.ToDouble() * Math.Pow(neutralPointSensorConfig.Pipe_Outside_Diameter, 2);
                //线重力
                double m = neutralPointSensorConfig.Pipe_Line_Weight;
                //井筒浮力 按水算 1000升=1kg/立方米  这里换算水的重量
                double p2 = 7.854 * Math.Pow(10, -4) * neutralPointSensorConfig.Wellbore_Fluid_Density * Math.Pow(neutralPointSensorConfig.Pipe_Outside_Diameter, 2);
                //管柱液体重力
                double p3 = 7.854 * Math.Pow(10, -4) * neutralPointSensorConfig.Pipe_Fluid_Density * Math.Pow(neutralPointSensorConfig.Pipe_Inside_Diameter, 2);
                //当上顶力=管柱重力（线重*L*重力加速度）+管柱内灌入流体重力-井筒内流体浮力   为中和点； 
                var mesureValue = f / (m - p2 + p3) * config.KValue;
                OutputValue = mesureValue;
                CurrentValue = mesureValue;
            }
            else
            {
                Status = DevStatus.S_UNKNOWN;
            }
        }
        public async Task CollectDataAsync()
        {
            
        }
    }
}
